<?php
	include('../service/userService.php');
	include('../service/productDetailService.php');

	if(isset($_POST['addUser'])) {
		try {
			if ($_POST['pass1'] == $_POST['pass2']) {
				if (isset($_POST['userid']) && $_POST['userid'] != '') {
					sUpdateUserInfo($_POST['userid'], $_POST['nick'], $_POST['name'], $_POST['type'], $_POST['pass1']);
					echo 'updated';
				} else {
					sAddUser($_POST['nick'], $_POST['name'], $_POST['type'], $_POST['pass1']);
					echo 'added';
				}
			} else {
				echo 'NOK';
			}
		} catch (Exception $e) {
			die ($e->getMessage());
			echo 'NOK';
		}
	} else if (isset($_POST['deleteUser'])) {
		try {
			sDeleteUser($_POST['deleteUser']);
			echo 'deleted';
		} catch (Exception $e) {
			echo 'NOK';
		}
	} else if (isset($_POST['uid'])) {
		$user = ConnectionManager::getInstance()->fetch(getUserById($_POST['uid']));
		$arr = array ('USER_ID' => $user[0], 'USER_NAME' => $user[1], 'USER_FULLNAME' => $user[2], 'USER_TYPE' => $user[3], 'USER_PASS' => $user[4]);
		echo json_encode($arr);
	} else if (isset($_POST['deleteAttr'])) {
		try {
			sDeleteAttribute($_POST['deleteAttr']);
			echo 'deleted';
		} catch (Exception $e) {
			echo 'NOK';
		}
	} else if (isset($_POST['addAttr'])) {
		try {
			if (isset($_POST['attrId']) && $_POST['attrId'] != '') {
				sUpdateAttribute($_POST['attrId'], $_POST['name'], $_POST['type'], $_POST['description'], $_POST['order']);
				echo 'updated';
			} else {
				sAddAttribute($_POST['name'], $_POST['type'], $_POST['description'], $_POST['order']);
				echo 'added';
			}
		} catch (Exception $e) {
			die ($e->getMessage());
			echo 'NOK';
		}
	} else if (isset($_POST['attrId']) && $_POST['attrId'] != '') {
		$attr = ConnectionManager::getInstance()->fetch(sLoadAttribute($_POST['attrId']));
		$arr = array('PRD_ID' => $attr[0], 'PRD_NAME' => $attr[1], 'PRD_TYPE' => $attr[2], 'PRD_SORTORDER' => $attr[3], 'PRD_DESCRIPTION' => $attr[4]);
		echo json_encode($arr);
	}
?>